#include "uti_interface.h"
#include "uti_consolecolor.h"
#include "uti_db.h"
using namespace std;


//
//BarSerInfo barserinfo;
//
//strcpy(barserinfo.barinfo.instru, m_pthreadcfg->drivebararr[i].instru);
//barserinfo.barinfo.type = Bartype::tick;
//barserinfo.barinfo.cnt = 1;
//barserinfo.sersize = 3600;
//barserinfo.p = pticksercallback_fun[i];
//
//bool GetBarSeries(const char* ins, Bartype type, unsigned int cnt, 
//					unsigned int agonum, BarSeries** ppbarser/*out*/)
//{
//
//}

//current interface only consider term less than one day although you could set unit term more than 1 day
//ie. set type:min typecnt:60 * 5, agoday:4,  the bar (60* 5 min) only contain mins in one day. and 4 bars included
//do this just because make sure the first bar of every has a significant start. otherwise, not
//Except that the bar unit is integter size of every day. for example (Bartype:day Barcnt: 2). consider it later
bool GetUserBarSeries(char* inst, Bartype type, unsigned int typecnt, unsigned int agoday, BarSeries** ppbarser/*out*/)
{
	BarSerInfo barserinfo;

	strcpy(barserinfo.barinfo.instru, inst);
	barserinfo.barinfo.type = type;
	barserinfo.barinfo.cnt = typecnt;	
	barserinfo.p = NULL;
	if (tick == barserinfo.barinfo.type)	                                             
		barserinfo.sersize = DEFAULT_TICKSERSIZE_OFDAY * (1 + agoday)/typecnt + 1;
	else if (sec == barserinfo.barinfo.type)										//because sec and min type is limit by day, so set (DEFAULT_SECSERSIZE_OFDAY/typecnt + 1)
		barserinfo.sersize = (DEFAULT_SECSERSIZE_OFDAY/typecnt + 1) * (1 + agoday); // to filter valid data in one day.
	else if (Bartype::min == barserinfo.barinfo.type)
		barserinfo.sersize = (DEFAULT_MINSERSIZE_OFDAY/typecnt + 1) * (1 + agoday);
	else if (day == barserinfo.barinfo.type)
		barserinfo.sersize = DEFAULT_DAYSERSIZE_OFDAY * (1 + agoday)/typecnt + 1;  //tick and day type is NOT limit by day, 

	//allocate memory
	if (false == GetBarSeries(&barserinfo, ppbarser))
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		cerr<<"[GetUserBarSeries] GetBarSeries Failed"<<endl;
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  

		return false;
	}

	//read file
	//DB(BarInfo* pbar, unsigned int rdfcnt, BarSeries** ppbarser);

	DB* pdb = new DB(&barserinfo.barinfo, agoday, ppbarser);
	if (NULL == pdb)
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		cerr<<"[GetUserBarSeries] pdb Failed"<<endl;
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  

		return false;
	}

	pdb->ReadData();




	if (pdb)
		delete pdb;


	return true;

}

bool GetBarSeries(BarSerInfo* pserinfo, BarSeries** ppbarser/*out*/)
{

	*ppbarser = new BarSeries(pserinfo);
	if (NULL == *ppbarser)
	{
		cerr<<"[GetBarSeries] Not enough memory for user bar series"<<endl;	
		return false;
	}

	////////////////////////////
	//use *ppbarser to read file at least agonum bar

	return true;
}

bool GetOrderSeries(OrderSerInfo* pserinfo, OrderSeries** pporderser/*out*/)//no need to read from file, so need for agonum
{

	*pporderser = new OrderSeries(pserinfo);

	if (NULL == *pporderser)
	{
		cerr<<"[GetOrderSeries] Not enough memory for user order series"<<endl;	
		return false;
	}


	return true;
}

bool GetOrderSeries(unsigned int sersize, void (*pcallback_fun)(), OrderSeries** pporderser/*out*/)//no need to read from file, so need for agonum
{
	OrderSerInfo orderserinfo;

	orderserinfo.sersize = sersize;
	orderserinfo.p = pcallback_fun;

	*pporderser = new OrderSeries(&orderserinfo);

	if (NULL == *pporderser)
	{
		cerr<<"[GetOrderSeries] Not enough memory for user order series"<<endl;	
		return false;
	}


	return true;
}


bool GetTradeSeries(TradeSerInfo* pserinfo, TradeSeries** pptradeser/*out*/)//no need to read from file, so need for agonum
{

	*pptradeser = new TradeSeries(pserinfo);

	if (NULL == *pptradeser)
	{
		cerr<<"[GetTradeSeries] Not enough memory for user trade series"<<endl;	
		return false;
	}
	return true;
}



bool GetTradeSeries(unsigned int sersize, void (*pcallback_fun)(), TradeSeries** pptradeser/*out*/)//no need to read from file, so need for agonum
{
	TradeSerInfo tradeserinfo;

	tradeserinfo.sersize = sersize;
	tradeserinfo.p = pcallback_fun;

	*pptradeser = new TradeSeries(&tradeserinfo);

	if (NULL == *pptradeser)
	{
		cerr<<"[GetTradeSeries] Not enough memory for user trade series"<<endl;	
		return false;
	}
	return true;
}




//bool GetMapSeries(OrderSysMapSerInfo* pserinfo, OrderSysMapSeries** ppmapser/*out*/)
//{
//	*ppmapser = new OrderSysMapSeries(pserinfo);
//
//	if (NULL == *ppmapser)
//	{
//		cerr<<"[GetMapSeries] Not enough memory for map series"<<endl;	
//		return false;
//	}
//	return true;
//}


bool GetIndexSeries(IndexSerInfo* pserinfo, IndexSeries** ppindexser/*out*/)
{
	*ppindexser = new IndexSeries(pserinfo);

	if (NULL == *ppindexser)
	{
		cerr<<"[GetIndexSeries] Not enough memory for index series"<<endl;	
		return false;
	}
	return true;
}



bool GetIndexSeries(unsigned int sersize, void (*pcallback_fun)(), IndexSeries** ppindexser/*out*/)
{
	IndexSerInfo indexserinfo;

	indexserinfo.sersize = sersize;
	indexserinfo.p = pcallback_fun;

	*ppindexser = new IndexSeries(&indexserinfo);

	if (NULL == *ppindexser)
	{
		cerr<<"[GetIndexSeries] Not enough memory for index series"<<endl;	
		return false;
	}
	return true;
}
